package cn.deali.homework9.utils;

import android.content.Context;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.deali.homework9.model.LocalMusic;
import cn.deali.homework9.model.OnlineMusic;
import cn.deali.homework9.model.TopMusicList;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OnlineMusicUtils {
    private static final String TAG = "OnlineMusicUtils";
    private static OkHttpClient client = new OkHttpClient();

    private static String get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static List<TopMusicList> getTopMusicList(Context context) {
        List<TopMusicList> list = new ArrayList<>();

        try {
            for (int idx = 0; idx <= 5; idx++) {
//            for (int idx = 0; idx <= 33; idx++) {
                String result = get("http://music.eleuu.com/top/list?idx=" + idx);
                JSONObject jsonObject = new JSONObject(result);
                if (!jsonObject.get("code").equals(200)) {
                    Log.e(TAG, "获取不到网易云音乐数据！");
                    return list;
                }

                JSONObject playlist = jsonObject.getJSONObject("playlist");
                TopMusicList topMusicList = new TopMusicList(
                        idx,
                        playlist.getString("name"),
                        playlist.getString("coverImgUrl"),
                        getOnlineMusic(context, idx)
                );

                list.add(topMusicList);
            }

        } catch (IOException | JSONException ex) {
            Log.e(TAG, ex.getMessage());
        }

        return list;
    }

    public static List<OnlineMusic> getOnlineMusic(Context context, int idx) {
        List<OnlineMusic> list = new ArrayList<>();

        try {
            String result = get("http://music.eleuu.com/top/list?idx=" + idx);
            JSONObject jsonObject = new JSONObject(result);
            if (!jsonObject.get("code").equals(200)) {
                Log.e(TAG, "获取不到网易云音乐数据！");
                return list;
            }

            JSONArray playList = jsonObject.getJSONObject("playlist").getJSONArray("tracks");
            for (int i = 0; i < playList.length(); i++) {
                JSONObject track = playList.getJSONObject(i);
                String id = track.getString("id");
                String name = track.getString("name");
                String picUrl = track.getJSONObject("al").getString("picUrl");
                StringBuilder artistBuilder = new StringBuilder();
                JSONArray artists = track.getJSONArray("ar");
                for (int artistIndex = 0; artistIndex < artists.length(); artistIndex++) {
                    JSONObject artistObj = (JSONObject) artists.get(artistIndex);
                    artistBuilder.append(artistObj.getString("name") + " ");
                }

                OnlineMusic onlineMusic = new OnlineMusic(id, name, artistBuilder.toString(), picUrl);

                list.add(onlineMusic);
            }

        } catch (IOException | JSONException ex) {
            Log.e(TAG, ex.getMessage());
        }

        return list;
    }
}
